package cn.minhx.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Redis 客户端，使用 Redisson
 * <a href="https://github.com/redisson/redisson">Redisson</a>
 *
 * @author min
 */
@Configuration
@EnableConfigurationProperties(RedisClientConfigProperties.class)
public class RedisClientConfig {

	@Bean("redissonClient")
	public RedissonClient redissonClient(RedisClientConfigProperties properties) {
		Config config = new Config();
		// 根据需要可以设定编解码器；https://github.com/redisson/redisson/wiki/4.-%E6%95%B0%E6%8D%AE%E5%BA%8F%E5%88%97%E5%8C%96
		config.setCodec(JsonJacksonCodec.INSTANCE);

		config.useSingleServer().setAddress("redis://" + properties.getHost() + ":" + properties.getPort())
				// .setPassword(properties.getPassword())
				.setConnectionPoolSize(properties.getPoolSize())
				.setConnectionMinimumIdleSize(properties.getMinIdleSize())
				.setIdleConnectionTimeout(properties.getIdleTimeout()).setConnectTimeout(properties.getConnectTimeout())
				.setRetryAttempts(properties.getRetryAttempts()).setRetryInterval(properties.getRetryInterval())
				.setPingConnectionInterval(properties.getPingInterval()).setKeepAlive(properties.isKeepAlive());

		return Redisson.create(config);
	}
}
